<?php

namespace common\models\shop;

use Yii;

/**
 * This is the model class for table "shop_goods_category".
 *
 * @property string $id
 * @property string $name
 * @property string $thumb
 * @property string $parentid
 * @property integer $isrecommand
 * @property string $description
 * @property integer $displayorder
 * @property integer $enabled
 */
class ShopGoodsCategory extends \yii\db\ActiveRecord
{

    //主要用于日志记录
    public $uselog = [
        'tabcomment' => '商品分类',      //用于修改了什么的显示
        'mainfield' => 'name',      //主要的显示标题的字段
        'picturefield' => ['thumb'],        //图片字段
        'timefield' => [],       //时间字段
        'unsetfiled' =>[],      //不需要记录的字段
    ];


    const ENABLE_ACTIVE = 1;
    const ENABLE_HIDDEN = 0;

    const  RECOMMEND_ACTIVE = 1;
    const  RECOMMEND_HIDDEN = 0;

    public static $enables = [
        self::ENABLE_ACTIVE => '显示',
        self::ENABLE_HIDDEN => '不显示'
    ];


    public static $recommends = [
        self::RECOMMEND_ACTIVE => '推荐显示',
        self::RECOMMEND_HIDDEN => '不推荐显示'
    ];



    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'shop_goods_category';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name'], 'required'],
            [['parentid', 'isrecommand', 'displayorder', 'enabled'], 'integer'],
            [['name'], 'string', 'max' => 50],
            [['description'], 'string', 'max' => 500],
            [['displayorder'],  'filter', 'filter' => 'intval'],
            [['thumb'], 'image', 'extensions' => 'jpeg,jpg,gif,png','on' =>['update','insert']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => '分类名称',
            'thumb' => '缩略图',
            'parentid' => '上级分类',
            'isrecommand' => '是否推荐显示',
            'description' => '简介',
            'displayorder' => '排序',
            'enabled' => '是否显示',
        ];
    }


    /**
     * @param $id
     * @return array|\yii\db\ActiveRecord[]
     * 获取视图层选择父类的数据
     */
    public static function selectViewParent($id)
    {
        $selcate = self::find()->select('id,name')->where('id != :id and parentid=0',[':id'=>$id])->asArray()->all();
        return array_merge([['id'=>0,'name'=>'顶级分类']],$selcate);
    }


    public function getParent()
    {
        return $this->hasOne(self::className(),['id'=>'parentid']);
    }



    /**
     * @return array|\yii\db\ActiveRecord[]
     * 取出所有分类
     */
    public static function allCate()
    {
        return self::find()->select('*')->where('enabled=:enabled',[':enabled'=>self::ENABLE_ACTIVE])->asArray()->all();
    }



}
